package messageFormatTest;

import java.io.UnsupportedEncodingException;
import java.text.MessageFormat;
import java.util.MissingResourceException;  
import java.util.ResourceBundle;  
  
public final class TestMessage {  
    private static final String BUNDLE_NAME = "messageFormatTest/LocalString";
    private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME);  
  
    public static String getFilledText(String key, Object... args) {
        String template = null;  
        try {  
            template = RESOURCE_BUNDLE.getString(key);

            System.out.println(  getEncoding(  template ));
            template=new String(template.getBytes("ISO8859-1"),"GB2312");
            System.out.println(  getEncoding(  template ));
        } catch (MissingResourceException e) {  
            StringBuilder b = new StringBuilder();  
            try {  
                b.append(RESOURCE_BUNDLE.getString("message.unknown"));  
                b.append(": ");  
            } catch (MissingResourceException e2) {}  
            b.append(key);  
            if (args != null && args.length > 0) {  
                b.append("(");  
                b.append(args[0]);  
                for (int i = 1; i < args.length; i++) {  
                    b.append(", ");  
                    b.append(args[i]);  
                }  
                b.append(")");  
            }  
            return b.toString();  
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return MessageFormat.format(template, args);  
    }  
      
   /* public static void main(String[] args) {
      
        System.out.println(get("error.scan",123,"aaa","bbb"));  
    }  */

    public static String getEncoding(String str) {
        String encode = "GB2312";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                String s = encode;
                return s;
            }
        } catch (Exception exception) {
        }
        encode = "ISO-8859-1";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                String s1 = encode;
                return s1;
            }
        } catch (Exception exception1) {
        }
        encode = "UTF-8";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                String s2 = encode;
                return s2;
            }
        } catch (Exception exception2) {
        }
        encode = "GBK";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                String s3 = encode;
                return s3;
            }
        } catch (Exception exception3) {
        }
        return "";
    }

}
